using System.Collections.Generic;
using System.Linq;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.WeChatMall.Domain.Entitys.GsInfo;
using ZilLion.Service.WeChatMall.Domain.Repositorys.Interface.GoodInfo;

namespace ZilLion.Service.WeChatMall.Domain.Repositorys.GoodInfo
{
    public class GsGroupReponsitory : SqlServerRespository<GsGroupEntity>, IGsGroupReponsitory
    {
        public GsGroupReponsitory()
        {
            Context = new WechatMallContext();
        }

        public IEnumerable<GsGroupEntity> GetGroups(string zlcustid)
        {
            var querysql = DapperHelper.CreateColSqlByMap<GsGroupEntity>();
            var sql = $@"SELECT {querysql} FROM gsgroupinfo a           
            WHERE a.zlcompanykey=@zlcustid";
            var result = GetList(sql, new {zlcustid});
            return result;
        }

        public IEnumerable<GsGroupEntity> GetGroupsByShopid(long shopid)
        {
            var querysql = DapperHelper.CreateColSqlByMap<GsGroupEntity>("a");
            var sql = $@" SELECT {querysql} FROM gsgroupinfo a
            INNER JOIN groupgsrelationship b ON b.groupid = a.groupid
            INNER JOIN shopgsrelationship c ON c.gsid = b.gsid
            WHERE c.shopid = @shopid";
            var result = GetList(sql, new {shopid});
            return result;
        }

        public GsGroupEntity GetGroupById(long groupid)
        {
            var querysql = DapperHelper.CreateColSqlByMap<GsGroupEntity>();
            var sql = $@"SELECT {querysql} FROM gsgroupinfo a                  
            WHERE a.groupid=@groupid";
            var result = GetList(sql, new {groupid}).FirstOrDefault();
            return result;
        }

        public void AddGsGroup(GsGroupEntity entity, UnitOfWork unit = null)
        {
            Add(entity, unit);
        }

        public void EditGsGroup(GsGroupEntity entity, UnitOfWork unit = null)
        {
            Modify(entity, unit);
        }

        public void RemoveGsGroup(GsGroupEntity entity, UnitOfWork unit = null)
        {
            Remove(entity, unit);
        }

        public void RemoveGsGroupById(long groupid, UnitOfWork unit = null)
        {
            var sql = $@"DELETE FROM gsgroupinfo                
            WHERE groupid=@groupid";
            DapperHelper.Execute(sql, Context, unit, new {groupid});
        }

        public GsGroupEntity GetGroupByGsid(IList<long> gsids)
        {
            var querysql = DapperHelper.CreateColSqlByMap<GsGroupEntity>();
            var condition = gsids.Aggregate(string.Empty, (current, gsid) => current + gsid + ",");
            if (condition.EndsWith(","))
                condition = condition.Left(condition.Length - 1);
            var sql = $@"SELECT {querysql} FROM gsgroupinfo a
            inner join groupgsrelationship b on a.groupid=b.groupid            
            WHERE b.gsid in ({condition})";
            var result = GetList(sql).FirstOrDefault();
            return result;
        }
    }
}